﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;
using Shohdi.DataHandler;

public partial class AreaControl : System.Web.UI.UserControl
{
   
    

   


    public delegate void ItemChangedDel (object sender,EventArgs e) ;


    public event ItemChangedDel ItemChanged = null;

    protected virtual void OnItemChanged( EventArgs e)
    {
        if (ItemChanged != null)
            ItemChanged(this, e);

    }


    public DropDownList DDLArea
    {
        get
        {
            return ddlArea;
        }
    }


    public DropDownList DDLSubarea
    {
        get
        {
            return ddlSubarea;
        }
    }

   

    /// <summary>
    /// this function will return a value of Area
    /// </summary>
    /// <returns>the id of the lowest degree selected Area</returns>
    public long get()
    {
        if (ddlArea.SelectedValue == "")
        {
            BindArea();
            
           
        }
        
           

           

            if (ddlSubarea.SelectedValue != "-1")
            {
                return long.Parse(ddlSubarea.SelectedValue);
            }

            if (ddlArea.SelectedValue != "-1")
            {
                return long.Parse(ddlArea.SelectedValue);
            }


        return -1;

        


    }




    /// <summary>
    /// this function will set the Area control by ID of lowest child
    /// </summary>
    /// <param name="AreaID">the ID to set control with</param>
    public void set(long AreaID)
    {
        AreaDB objAreaDB = new AreaDB(ConnectionStrings.LMS);
        List<Area> lstArea = objAreaDB.getAreaByID(AreaID);
        if (lstArea.Count == 0)
        {
            if (ddlArea.Items.Count == 0)
                BindArea();
            ddlArea.SelectedValue = "-1";
            ddlArea_SelectedIndexChanged(this, new EventArgs());

            return;


          
        }

       

        


        while (lstArea[0].ParentID != null)
        {
            List<Area> lstParent = objAreaDB.getAreaByID(lstArea[0].ParentID.Value);
            if (lstParent.Count > 0)
            {
                lstArea.Insert(0, lstParent[0]);
            }

        }

        try
        {
            if (ddlArea.Items.Count == 0)
                BindArea();

            ddlArea.SelectedValue = lstArea[0].ID.ToString();
            bindSubArea();
            ddlSubarea.SelectedValue = lstArea[1].ID.ToString();
            

        }
        catch
        {
        }



    }




   





    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (isParent)
            //{
            //    ddlDivision.AutoPostBack = false;
            //    ddlSection.Visible = false;
                
            //}
            BindArea();
            bindSubArea();
           

            OnItemChanged(new EventArgs());
        }
    }

    


    /// <summary>
    /// this function will fill the Area from database
    /// </summary>
    protected void BindArea()
    {
        Area objArea = new Area();
        objArea.ParentID = null;

        AreaDB objAreaDB = new AreaDB(ConnectionStrings.LMS);
        List<Area> lstArea = objAreaDB.Select("", " where [ParentID] is NULL ");

        

       

        ddlArea.DataSource = lstArea;
        ddlArea.DataTextField = "AreaNameEN";
        ddlArea.DataValueField = "ID";
        ddlArea.DataBind();
    }




    /// <summary>
    /// this function will fill the sub area from database
    /// </summary>
    protected void bindSubArea()
    {
        Area objArea = new Area();
        objArea.ParentID = long.Parse(ddlArea.SelectedValue);

        AreaDB objAreaDB = new AreaDB(ConnectionStrings.LMS);
        List<Area> lstArea = objAreaDB.Search(objArea);

        lstArea.Insert(0, new Area("-- اختار --", "-- Select --", null, ""));

        


       

        ddlSubarea.DataSource = lstArea;
        ddlSubarea.DataTextField = "AreaNameEN";
        ddlSubarea.DataValueField = "ID";
        ddlSubarea.DataBind();
    }


   



   



    



    




    protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindSubArea();
       
        OnItemChanged(e);
    
    }


    protected void ddlSubarea_SelectedIndexChanged(object sender, EventArgs e)
    {
        

        OnItemChanged(e);



    }
   
   
}
